﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MS_Entity;




namespace MS_UI
{
    public partial class Frm_NewVar_Mod : Form
    {
        public Frm_NewVar_Mod()
        {
            InitializeComponent();

            //初始化
            this.Load += Frm_NewVar_Mod_Load;

        }



        //创建变量实例
       public  Variable_Modbus var;
        //创建报警变量实例

       public VarAlarm_Modbus varAlarm;



        //这个是自己写的
        void Frm_NewVar_Mod_Load(object sender, EventArgs e)
        {
            //初始化ComboBox下拉菜单

            //1类型
            this.cmb_DataType.Items.Add("Bool");
            this.cmb_DataType.Items.Add("Signed");
            this.cmb_DataType.Items.Add("Unsigned");
            this.cmb_DataType.Items.Add("Hex");
            this.cmb_DataType.Items.Add("Long");
            this.cmb_DataType.Items.Add("Long Inverse");
            this.cmb_DataType.Items.Add("Float");
            this.cmb_DataType.Items.Add("Float Inverse");
            this.cmb_DataType.Items.Add("Double");
            this.cmb_DataType.Items.Add("Double Inverse");
            this.cmb_DataType.SelectedIndex = 6;



            //存储区
            this.cmb_StoreArea.Items.Add("01 Coil Status(0X)");
            this.cmb_StoreArea.Items.Add("02 Input Status(1X)");
            this.cmb_StoreArea.Items.Add("03 Holding Register(4X)");
            this.cmb_StoreArea.Items.Add("04 Input Registers(3X)");
            this.cmb_StoreArea.SelectedIndex = 2;


            //初始化时，下面勾选框都是禁用的


            this.chk_High.Enabled = false;
            this.chk_HiHi.Enabled = false;
            this.chk_Low.Enabled = false;
            this.chk_LoLo.Enabled = false;

        }




     

        private void btn_OK_Click(object sender, EventArgs e)
        {

            //modbus变量实例

             var =new Variable_Modbus();
            var.VarName = this.txt_VarName.Text.Trim();
            var.StoreArea = this.cmb_StoreArea.Text.Trim();
            var.DataType=this.cmb_DataType.Text.Trim();
            var.Address=this.txt_Address.Text.Trim();
            var.IsFiling = this.chk_IsFiling.Checked ? "1" : "0";
            var.IsAlarm = this.chk_IsAlarm.Checked ? "1" : "0";
            var.IsReport=this.chk_IsReport.Checked ? "1" : "0";
            var.Note=this.txt_Note.Text.Trim();

            //modbus变量报警实例


            if (this.chk_High.Checked | this.chk_HiHi.Checked | this.chk_LoLo.Checked | this.chk_Low.Checked)
            { 
            
            varAlarm=new VarAlarm_Modbus();
                varAlarm.VarName=this.txt_VarName.Text.Trim();


                if (chk_High.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_LoLo.Text.Trim()),
                        AlarmType = "High",
                        AlarmVaule = float.Parse(this.txt_Alarm_LoLo.Text.Trim()),
                        Note = this.txt_Note_High.Text.Trim(),

                    });


                }

                if (chk_HiHi.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_HiHi.Text.Trim()),
                        AlarmType = "HiHi",
                        AlarmVaule = float.Parse(this.txt_Alarm_HiHi.Text.Trim()),
                        Note = this.txt_Note_HiHi.Text.Trim(),

                    });


                }

                if (chk_Low.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_Low.Text.Trim()),
                        AlarmType = "Low",
                        AlarmVaule = float.Parse(this.txt_Alarm_Low.Text.Trim()),
                        Note = this.txt_Note_Low.Text.Trim(),

                    });


                }


                if (chk_LoLo.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_LoLo.Text.Trim()),
                        AlarmType = "LoLo",
                        AlarmVaule = float.Parse(this.txt_Alarm_LoLo.Text.Trim()),
                        Note = this.txt_Note_LoLo.Text.Trim(),

                    });


                }

            }


            this.DialogResult = DialogResult.OK;
            this.Close();


        }



        #region 变化事件

        /// <summary>
        /// 事件需要在控件属性里更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chk_IsAlarm_CheckedChanged(object sender, EventArgs e)
        {
            //报警勾选了才能用这几个选项功能

            bool value=this.chk_IsAlarm.Checked;
            this.chk_High.Enabled = value;
            this.chk_HiHi.Enabled = value;
            this.chk_Low.Enabled = value;
            this.chk_LoLo.Enabled = value;


        }


        

        //勾选了高中低的做法
        private void chk_LoLo_CheckedChanged(object sender, EventArgs e)
        {

            bool value = this.chk_LoLo.Checked;
           txt_Alarm_LoLo.Enabled= value;
            txt_Priority_LoLo.Enabled = value;
            txt_Note_LoLo.Enabled = value;


        }

        private void chk_Low_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_Low.Checked;
            txt_Alarm_Low.Enabled = value;
            txt_Priority_Low.Enabled = value;
            txt_Note_Low.Enabled = value;
        }

        private void chk_HiHi_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_HiHi.Checked;
            txt_Alarm_HiHi.Enabled = value;
            txt_Priority_HiHi.Enabled = value;
            txt_Note_HiHi.Enabled = value;
        }

        private void chk_High_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_High.Checked;
            txt_Alarm_High.Enabled = value;
            txt_Priority_High.Enabled = value;
            txt_Note_High.Enabled = value;
        }

        #endregion

        


    }
}
